﻿using NfsBLL;
using NfsCommon.Util;
using NfsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MobileWeb.Controllers
{
    public class SqauthorizationController : Controller
    {
        //
        // GET: /Sqauthorization/
        public ActionResult Index()
        {
            int id = int.Parse(CookieHelper.GetDecodeCookie("nfs_UserId"));
            AgencyBLL agbll = new AgencyBLL();
            agency model = agbll.GetModel_account(id);
            if (model != null && model.end_time != null)
            {
                if (model.end_time > DateTime.Now)
                {
                    DateTime d1 =DateTime.Parse(model.end_time.ToString());
                    DateTime d2 = DateTime.Now;

                    int days = (d1-d2).Days;
                    ViewData["ady"] = days;
                    ViewData["mes"] = "2";
                }
                else
                {

                    ViewData["mes"] = "0";
                }
            }
            else
            {

                ViewData["mes"] = "1";
            }
            return View();
        }
        #region 更新授权时间申请
        [HttpPost]
        public JsonResult Add_Sqauthorization(DateTime start_time)
        {
            try
            {
                Sq_authorizationBLL bll = new Sq_authorizationBLL();
                int id = int.Parse(CookieHelper.GetDecodeCookie("nfs_UserId"));
                AgencyBLL agbll = new AgencyBLL();
                agency model = agbll.GetModel_account(id);
                if (model != null)
                {
                    sq_authorization sq_model = new sq_authorization { agency_id = model.id, start_time = start_time, end_time = start_time.AddDays(90), state = "0", valid = 1, time = DateTime.Now };
                    bool flag = bll.Add(sq_model);
                    if (flag)
                    {
                        return Json(flag, JsonRequestBehavior.AllowGet);

                    }
                    else
                    {
                        return Json("error", JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {
                    return Json("error", JsonRequestBehavior.AllowGet);

                }

            }
            catch (Exception)
            {

                return Json("error", JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

    }
}
